package com.pdstudio.carrecom.ui.activity.more;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.UserProtocal;

/**
 * 用户协议
 * 类: ActivityUserProtocal <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-21 下午4:39:14
 */
public class ActivityUserProtocal extends Activity {
	
	private TextView mTitle,mContent;
	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_userprotocal);

		initTitle();
		InitialView();
	}
	
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("用户协议");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}

	
	private void InitialView(){
		
		mTitle=(TextView)findViewById(R.id.more_userprotocal_title);
		mContent=(TextView)findViewById(R.id.more_userprotocal_content);
	
		try {
					HttpExecuteJson http = new HttpExecuteJson(this, mUserProtocalListener);
					Map<String, Object> rps = new HashMap<String, Object>();
					rps.put("uid", AppContext.getInstance()._userName);
					http.get(ServiceHelper.UserProtocal, rps);
				} catch (Exception e) {
					e.printStackTrace();
					mTitle.setText("无");
				}
				
	}
	
	private void DoJson(String json){
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<ResultInfo>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					UserProtocal up=new Gson().fromJson(new Gson().toJson(edv.data), UserProtocal.class);
					mTitle.setText(up.title);
					mContent.setText(up.content);
				}catch(Exception e){
					e.printStackTrace();
					mTitle.setText("无");
				}
			}else{
				mTitle.setText("无");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mTitle.setText("无");
		}

	}
	
	private httpReturnJson mUserProtocalListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityUserProtocal.this, "获取用户协议失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityUserProtocal.this, "获取用户协议退出");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

	
}
